//
//  ViewController.m
//  SpotifySearchSample
//
//  Created by Christian Braunschweiler on 21.04.13.
//  Copyright (c) 2013 sample. All rights reserved.
//

#import "SPSession.h"
#import "ViewController.h"
#import "appkey.h"
#import "SPLoginViewController.h"
#import "SPAsyncLoading.h"
#import "SpotifySearchHandler.h"

@interface ViewController ()

@property (nonatomic, strong) SpotifySearchHandler *searchHandler;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

    self.searchHandler = [[SpotifySearchHandler alloc]init];
    self.searchHandler.delegate = self;

    [SPSession initializeSharedSessionWithApplicationKey:[NSData dataWithBytes:&g_appkey length:g_appkey_size] userAgent:@"rippleEffect.ShareSpot" loadingPolicy:SPAsyncLoadingImmediate error:nil];
    
    [[SPSession sharedSession] setDelegate:self];
}

-(void)viewDidAppear:(BOOL)animated{
   /* SPLoginViewController *controller = [SPLoginViewController loginControllerForSession:[SPSession sharedSession]];
    controller.allowsCancel = NO;
    controller.loginDelegate = self;
    [self presentModalViewController:controller animated:NO];*/
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//SPSessionDelegate mthods
-(void)sessionDidLoginSuccessfully:(SPSession *)aSession; {
    NSLog(@"Logged in successfully.");
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Yay!" message:@"You've logged in successfully!" delegate:nil cancelButtonTitle:@"Ok!" otherButtonTitles:nil];
    [alertView show];
    self.searchTextField.alpha = 1;
    self.searchTextField.userInteractionEnabled = YES;
    self.searchButton.alpha = 1;
    self.searchButton.userInteractionEnabled = 1;
}

-(void)session:(SPSession *)aSession didFailToLoginWithError:(NSError *)error; {
    // Invoked by SPSession after a failed login.
    NSLog(@"Error message: %@", [error localizedDescription]);
}

-(void)sessionDidLogOut:(SPSession *)aSession {


}

-(void)session:(SPSession *)aSession didEncounterNetworkError:(NSError *)error; {}
-(void)session:(SPSession *)aSession didLogMessage:(NSString *)aMessage; {}
-(void)sessionDidChangeMetadata:(SPSession *)aSession; {}

-(void)session:(SPSession *)aSession recievedMessageForUser:(NSString *)aMessage; {
    return;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message from Spotify"
                                                    message:aMessage
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

- (IBAction)loginPressed:(id)sender {
    [[SPSession sharedSession] attemptLoginWithUserName:self.usernameTextField.text
                                               password:self.passwordTextField.text];
}
- (IBAction)performSearch:(id)sender {
    [self.searchHandler performSearch:self.searchTextField.text];
}

//SpotifySearchHandler delegate methods
-(void)searchCompletedWithResult:(NSArray *)result {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Search complete" message:@"Search has completed." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
}
@end
